﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AgileTimeTracker.Client.Wpf.TimeControlModule;
using AgileTimeTracker.Domain.Exceptions;
using AgileTimeTracker.Domain.TaskModule;
using AgileTimeTracker.Domain.TfsModule;
using AgileTimeTracker.Domain.UserModule;

namespace AgileTimeTracker.Client.Wpf.Services
{
    public class AppService : BaseService
    {
        private static AppService instance;

        public static AppService Instance
        {
            get
            {
                return instance;
            }
        }

        static AppService()
        {
            instance = new AppService();
        }

        private AppService()
        {

        }

        public User Login(string login, string password)
        {
            var user = this.UserRepository.Query().FirstOrDefault(t => t.Login == login && t.Password == password);
            return user;
        }

        public User CreateUser(string login, string password)
        {
            var user = new User
            {
                Login = login,
                Password = password
            };

            this.UserRepository.Add(user);
            this.UserRepository.Commit();

            return user;
        }

        public void SaveUser(User user)
        {
            this.UserRepository.Update(user);
            this.UserRepository.Commit();
        }

        public List<TaskWorkTimeItemViewModel> GetTasks(UserProject userProject)
        {
            return userProject.TfsTasks.Select(t => new TaskWorkTimeItemViewModel(t)).ToList();
        }

        public BaseWorkItem GetTfsWorkItem(UserProject userProject, int workItemId)
        {
            this.TfsRepository.SetProject(userProject);
            return this.TfsRepository.GetWorkItem(workItemId);
        }

        public void UpdateTaskFromTfs(UserProject userProject, TfsTask tfsTask)
        {
            if (!tfsTask.TfsWorkItemId.HasValue)
            {
                return;
            }

            var workItem = this.GetTfsWorkItem(userProject, tfsTask.TfsWorkItemId.Value);
            tfsTask.IterationPath = workItem.IterationPath;
            tfsTask.RemainingWork = workItem.RemainingWork;
            tfsTask.Title = workItem.Title;
            tfsTask.UserStory = workItem.UserStory;
            tfsTask.EstimatedWork = workItem.OriginalEstimate;
            tfsTask.CompletedWork = workItem.CompletedWork;
        }

        public void SafeTfsWorkItem(UserProject userProject, TfsTask tfsTask)
        {
            var workItem = this.GetTfsWorkItem(userProject, tfsTask.TfsWorkItemId.Value);                        
            workItem.CompletedWork = tfsTask.CompletedWork;
            workItem.RemainingWork = tfsTask.RemainingWork;
            if (workItem.CompletedWork < 0)
            {
                throw new DomainException("Completed Work must be greater or equal to zero.");
            }

            if (workItem.RemainingWork < 0)
            {
                throw new DomainException("Remaining Work must be greater or equal to zero");
            }

            this.TfsRepository.SaveWorkItem(workItem);
        }
    }
}
